<!--
 * @Author       : wfl
 * @LastEditors: ww
 * @description  :
 * @updateInfo   :
 * @Date         : 2022-11-23 16:49:28
 * @LastEditTime: 2023-06-28 15:49:58
-->
<script lang="ts" setup name="TriggerNode">
import Node from '@main/views/approve-workflow/WorkFlow/tree-node/Node.vue'
import { computed, reactive } from 'vue'
import { isNotEmpty } from '@g/utils'
import { ETrigger } from '@main/views/approve-workflow/enum'

const props = defineProps({
  config: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const emits = defineEmits<{
  (e: 'selected'): void
  (e: 'insertNode', val: string): void
  (e: 'delNode', node: any): void
}>()

const state = reactive({
  showError: false,
  errorInfo: ''
})

const content = computed(() => {
  return props.config.props.type
})

// 校验数据配置的合法性
const validate = (err: string[]) => {
  state.showError = false
  if (props.config.props.type === ETrigger.发送请求) {
    if (isNotEmpty(props.config.props.http.url)) {
      state.showError = false
    }
    else {
      state.showError = true
      state.errorInfo = '请设置WEBHOOK的URL地址'
    }
  }
  else if (props.config.props.type === ETrigger.发送邮件) {
    if (
      !isNotEmpty(props.config.props.email.subject)
      || props.config.props.email.to.length === 0
      || !isNotEmpty(props.config.props.email.content)
    ) {
      state.showError = true
      state.errorInfo = '请设置邮件发送配置'
    }
    else {
      state.showError = false
    }
  }
  if (state.showError)
    err.push(`${props.config.name} 触发动作未设置完善`)

  return !state.showError
}

const handSelected = () => {
  emits('selected')
}
const handInsertNode = (type: string) => {
  emits('insertNode', type)
}
const handDelNode = () => {
  emits('delNode', props?.config)
}

defineExpose({ validate })
</script>

<template>
  <Node
    :title="config.name"
    :show-error="state.showError"
    :content="content"
    :error-info="state.errorInfo"
    placeholder="请设置触发器"
    header-bgc="#47bc82"
    header-icon="ikflow-chufa"
    @selected="handSelected"
    @delNode="handDelNode"
    @insertNode="(type: string) => handInsertNode(type)"
  />
</template>
